<?php
class ManagerController extends Controller {
	var $site_info;
	var $user_info;
	function ManagerController(){
		parent::Controller();
		global $site_info;
		global $visitor;
		$this->site_info = $site_info;
		
		$this->module = 'manager';
		
		$this->user_info = get_session('visitor');
		
		$public_requests = array(
			'site' => array('login', 'logout'),
		);
		if(!array_key_exists(APP, $public_requests) || !in_array(ACT, $public_requests[APP])){
			if(empty($this->user_info) || $this->user_info['has_login'] != true){
				$this->auto_login();
				//重新转向登录界面
				$this->redirect('/manager.php?act=login');
			}else{
				global $session_key;
				$session_expire = time() + intval(SESSION_EXPIRE);
				UCenter::update_user_state($this->user_info['_id'], $session_key, $session_expire);
			}
		}
		
		//初始化当前编辑语言
		$cur_lang = self::get_post_val('lang', LANGUAGE);
		//判断是否合法
		if(!Lang::is_lang($cur_lang)){
			$cur_lang = LANGUAGE;
		}
		$this->assign('cur_lang', $cur_lang);
		
		$langs = array();
		foreach ($this->site_info['langs'] as $lan=>$is_default){
			$langs[$lan] = Lang::get_lan_name($lan);
		}
		$this->assign('site_langs', $langs);
	}

	/**
	 * 尝试自动登录。使用单点登录的功能，将其它系统中的ticket传过来后，进行验证登录
	 */
	private function auto_login(){
		if(isset($_REQUEST['ticket'])){
			$ticket = trim($_REQUEST['ticket']);
			//解密
			$ticket_str = decrypt($ticket);
			list($session_id, $site_id, $user_id) = explode(';', $ticket_str);
			//依赖于UCenter的验证系统，可以通过UCenter进行单点登录
			$user = UCenter::get_user_info($site_id, $user_id);
			if(!empty($user)){
				//验证Session是否过期
				if($user['session_id']===$session_id && $user['session_expire']>time()){
					//合法，登录成功
					$this->do_login($user, $session_id);
					self::redirect('/manager.php');
				}
			}
		}
	}
	
	protected function do_login($user, $new_session_key=null){
		$user['has_login'] = true;
		unset($user['error_count']);
		unset($user['captcha_request']);

		//更新Session过期日期
		$session_expire = time() + intval(SESSION_EXPIRE);
		
		//生成ticket,作为单点登录凭证
		global $session_key;
		if(!empty($new_session_key)){
			$session_key = $new_session_key;
			setcookie('SESSION_ID', $session_key , $session_expire, '/');
		}
		
		//保存Session信息
		set_session('visitor', $user);
	}

	protected function error($msg, $links=array()){
		echo $msg.'<br>';
		foreach ($links as $link){
			echo '<a href="'.$link['url'].'">'.$link['text'].'</a><br>';
		}
		exit;
	}
	
	protected function info($msg, $links=array()){
		echo $msg;
		foreach ($links as $link){
			echo '<a href="'.$link['url'].'">'.$link['text'].'</a><br>';
		}
		exit;
	}

	protected function get_request_val($name, $default_value=''){
		if(isset($_REQUEST[$name])){
			return $_REQUEST[$name];
		}else{
			return $default_value;
		}
	}
	
	protected function get_request_int_val($name, $default_value=0){
		if(isset($_REQUEST[$name])){
			return intval($_REQUEST[$name]);
		}else{
			return $default_value;
		}
	}

	/**
	 * 从Requst的Get方法中获取值
	 * @param unknown_type $get_name
	 * @param unknown_type $default_value
	 */
	protected function get_val($get_name, $default_value=''){
		if(isset($_GET[$get_name])){
			return $_GET[$get_name];
		}else{
			return $default_value;
		}
	}
	
	/**
	 * 从Requst的Get方法中获取数值
	 * @param unknown_type $get_name
	 * @param unknown_type $default_value
	 */
	protected function get_int_val($name, $default_value=0){
		if(isset($_GET[$name])){
			return intval($_GET[$name]);
		}else{
			return $default_value;
		}
	}
	
	protected function get_post_val($name, $default_value=''){
		if(isset($_POST[$name])){
			return $_POST[$name];
		}else{
			return $default_value;
		}
	}
	
	protected function get_post_int_val($name, $default_value=0){
		if(isset($_POST[$name])){
			return intval($_POST[$name]);
		}else{
			return $default_value;
		}
	}
}

?>